package com.sls.web.manager.game.service.impl;

import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.EbaimiCheckFileInfo;
import com.sls.web.manager.game.pojo.EbaimiProduct;
import com.sls.web.manager.game.pojo.wogreat.WogreatProduct;
import com.sls.web.manager.game.querybean.EbaimiProductQuery;
import com.sls.web.manager.game.service.EbaiMiService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Service("ebaiMiService")
public class EbaiMiServiceImpl extends SuperService implements EbaiMiService {

	private Gson gson;

	public EbaiMiServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public Pagination<EbaimiProductQuery> queryProPagination(
			EbaimiProductQuery proQuery, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020957");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, proQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<EbaimiProductQuery> page = gson.fromJson(body,
					new TypeToken<Pagination<EbaimiProductQuery>>() {
					}.getType());
			return page;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public EbaimiProduct queryById(String id, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021000");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, id);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				EbaimiProduct pro = gson.fromJson(body, EbaimiProduct.class);
				return pro;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void updatePro(EbaimiProduct pro, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020958");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, pro);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void sync(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020951");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	/**
	 * 商品审核
	 */
	@Override
	public void confirmAudit(String supplierId, int priceRatio,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020955");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		EbaimiProduct wp = new EbaimiProduct();
		wp.setSupplierId(supplierId);
		wp.setPriceRatio(priceRatio);
		wp.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOnline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020956");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOffline(String state, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("020956");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		WogreatProduct wp = new WogreatProduct();
		wp.setOperator(userInfoVO.getUserName());
		wp.setStatus(state);
		SocketDTO dto = super.send(head, wp);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public List<EbaimiProduct> queryAllProduct(UserInfoVO userInfoVO) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 余额查询
	 * 
	 * @param userInfoVO
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	@Override
	public EbaimiProduct queryBalance(EbaimiProductQuery proQuery,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021003");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, proQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				EbaimiProduct ebInfo = gson.fromJson(body, EbaimiProduct.class);
				return ebInfo;
			} else {
				throw new ManagerException("未知错误！");
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 订单结果查询
	 * 
	 * @param proQuery
	 * @param userInfoVO
	 * @return
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	@Override
	public EbaimiProduct queryOrder(EbaimiProductQuery proQuery,
			UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021002");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SocketDTO dto = super.send(head, proQuery);
		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body) && !body.equals("\"\"")) {
				EbaimiProduct ebInfo = gson.fromJson(body, EbaimiProduct.class);
				return ebInfo;
			} else {
				throw new ManagerException("未知错误！");
			}
		}
		return null;
	}

	/**
	 * 商品新增
	 * 
	 * @param ebaimiProduct
	 * @param userInfoVO
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	@Override
	public void saveGoods(EbaimiProduct ebaimiProduct, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.EBAIMI_GOODS_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SocketDTO dto = super.send(head, ebaimiProduct);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 商品编辑
	 * 
	 * @param ebaimiProduct
	 * @param userInfoVO
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	@Override
	public void updateGoods(EbaimiProduct ebaimiProduct, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.EBAIMI_GOODS_ADD);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		SocketDTO dto = super.send(head, ebaimiProduct);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	/**
	 * 商品删除
	 * 
	 * @param ebaimiProduct
	 * @param userInfoVO
	 * @author Wangxuegang
	 * @date 2013-05-11
	 */
	@Override
	public void deleteGoods(String id, UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021006");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		EbaimiProduct info = new EbaimiProduct();
		info.setId(id);
		SocketDTO dto = super.send(head, info);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 合作方的易百米商户ID--查询
	 * 
	 * @param ebaimiProduct
	 * @param userInfoVO
	 * @author Wangxuegang
	 * @date 2013-05-13
	 */
	@Override
	public EbaimiProduct queryOid_loginid(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021008");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);

		if (dto != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			EbaimiProduct pro = gson.fromJson(dto.getBody(),
					EbaimiProduct.class);
			return pro;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 对账信息同步
	 * 
	 * @param userInfoVO
	 */
	@Override
	public void sendCheckFile(UserInfoVO userInfoVO) {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021004");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 对账文件导出
	 * 
	 * @param EbaimiCheckFileInfo
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	@Override
	public List<EbaimiCheckFileInfo> queryProPaginationOfBill(String billDate,
			UserInfoVO userInfoVO) throws Exception {
		EbaimiCheckFileInfo billInfo = new EbaimiCheckFileInfo();
		billInfo.setCurrentPage(1);
		billInfo.setPageSize(20);
		billInfo.setBillDate(billDate);
		List<EbaimiCheckFileInfo> billLinkList = new LinkedList<EbaimiCheckFileInfo>();
		Pagination<EbaimiCheckFileInfo> page = queryByPage(billInfo, userInfoVO);
		if (page != null) {
			if (page.getList() != null) {
				billLinkList.addAll(page.getList());
			}
			int count = page.getRowCount();
			if (billInfo.getPageSize() == 0) {
				billInfo.setPageSize(1);
			}
			for (int i = 1; i < Math.ceil(count * 1.0 / billInfo.getPageSize()); i++) {
				billInfo.setCurrentPage(i + 1);
				page = queryByPage(billInfo, userInfoVO);
				if (page.getList() != null) {
					billLinkList.addAll(page.getList());
				}
			}
			return billLinkList;
		}

		return null;
	}

	/**
	 * 对账文件分页查询
	 * 
	 * @param info
	 * @param userInfoVO
	 * @return
	 * @throws Exception
	 */
	public Pagination<EbaimiCheckFileInfo> queryByPage(
			EbaimiCheckFileInfo info, UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode("021010");
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, info);

		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			Pagination<EbaimiCheckFileInfo> order = gson.fromJson(body,
					new TypeToken<Pagination<EbaimiCheckFileInfo>>() {
					}.getType());
			return order;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}
}
